package com.hl.card.modular.packBack.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.hl.card.common.remote.resolve.RemoteService;
import com.hl.card.common.remote.user.RemoteUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.constant.tips.SuccessTip;
import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.Recharge;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.shiro.ShiroKit;
import com.hl.card.modular.pack.service.RechargePaymentService;
import com.hl.card.modular.pack.service.RechargeService;
import com.hl.card.modular.packBack.service.PartnerListService;

@Controller
@RequestMapping("/rechargeList")
public class RechargeListController extends BaseController {

	@Autowired
	RemoteService remoteService;


	private Logger logger= LoggerFactory.getLogger(RechargeListController.class);
    @Resource
    private PartnerListService partnerListService;

    private String PREFIX = "/packback/rechargeInfo/";
    private final String EXTRA = "RECHARGE";
    private final String EDIT_PAGE = "rechargeList_edit.html";
    private final String INDEX_PAGE = "/rechargeList";
    private final String LIST_PAGE = "rechargeList.html";
	private final String MANUAL = "recharge_manual.html";
	private final String MANUAL_READY = "recharge_manual_ready.html";
//    @Autowired
//    private RechargePayMgrService rechargePayMgrService;

    @Autowired
    private RechargePaymentService rechargePaymentService;

    @Autowired
    private RechargeService rechargeService;
    
    @RequestMapping("")
//    @RequestMapping("/index.html")
    public String index(HttpServletRequest request, ModelMap model){
    	
    	String toPage=INDEX_PAGE;
//    	String toPage="/rechargeList/"+INDEX_PAGE;
		model.put("notify", "充值审核操作");
    	String page =baseCheckAccess(request, toPage, "/rechargeList", model);
//    	String page = toPage;
    	if (!toPage.equals(page)) {
    		return page;
		}
    	return PREFIX+LIST_PAGE;
    }

    /**
     * 跟单
     */
    @RequestMapping(value = "/doc")
    @ResponseBody
    public Object doc(/*@Validated(value=RechargeDocCheck.class)*/Recharge recharge){
    	recharge.setExtra(EXTRA);
    	return rechargeService.doc(recharge);
    }

    /**
     * 跳转到修改申请页面
     */
    @RequestMapping(value = "/recharge_update/{recharge_no}")
    public Object joinRebateUpdate(@PathVariable String recharge_no, ModelMap model,HttpServletRequest request){
    	String toPage=PREFIX+EDIT_PAGE;
    	String page =baseCheckAccess(request, toPage, model);
//    	String page = toPage;
    	if (!toPage.equals(page)) {
    		return page;
		}

    	Recharge recharge= new Recharge(recharge_no);
    	recharge.setExtra(EXTRA);
    	Map<String, Object> map = rechargeService.select(recharge);
    	
    	model.addAttribute("rechargeList",map);
    	LogObjectHolder.me().set(map);
    	
    	return toPage;
    }


    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Recharge recharge) {
    	recharge.setExtra(EXTRA);
    	//对所有超过一个
    	Page<Recharge> dPage = rechargeService.selectMultiCondition(recharge);
    	Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", dPage.getTotal());
		jsonMap.put("rows", dPage.getRecords());
        return jsonMap;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Recharge recharge){
    	SuccessTip su = new SuccessTip();
    	Recharge rechargeSelect= new Recharge();
    	rechargeSelect.setDoc_account(ShiroKit.getUser().getAccount());
    	rechargeSelect.setRecharge_no(recharge.getRecharge_no());
//    	Recharge tempRecharge= rechargeService.selectMultiConditionOneForUpdate(rechargeSelect);
		Recharge tempRecharge= rechargeService.selectById(recharge.getRecharge_no());
    	if (tempRecharge==null || tempRecharge.getStatus()==3|| tempRecharge.getStatus()!=2) {
			su.setMessage("无法操作");
			su.setCode(2);
		}else {
			recharge.setAmount(tempRecharge.getAmount());
			if(recharge.getAmount()==null&&recharge.getActual_amount()==null){
				System.out.println("无效操作 金额不能为空");
				System.out.println("recharge.getAmount()"+recharge.getAmount());
				System.out.println("recharge.getActual_amount()"+recharge.getActual_amount());
			}
			if(recharge.getActual_amount()==null){
				recharge.setActual_amount(recharge.getAmount());
			}
			logger.info("审核参数："+recharge.getRecharge_no());
	        String data =rechargePaymentService.doTrans(recharge.getRecharge_no(),recharge.getActual_amount().intValue()+"");
			logger.info("审核结果:"+data);
	        JSONObject jsonObject = JSONObject.parseObject(data);
	        if ("SUCCESS".equals(jsonObject.get("resCode"))) {
	        	recharge.setAudit_time(new Date());
	        	recharge.setStatus(4);
	        	rechargeService.auditing(recharge);
			}else {
				su.setMessage("操作失败");
				su.setCode(2);
			}
		}
    	return su;
    }
    /**
     * 合同详情
     */
    @RequestMapping(value = "/rechargeDetail/{recharge_no}")
    public Object rechargeDetail(@PathVariable String recharge_no, Model model){
    	Recharge recharge= new Recharge(recharge_no);
    	recharge.setExtra(REDIRECT);
    	Map<String, Object> map = rechargeService.select(recharge);
    	model.addAttribute("rechargeList",map);
        return PREFIX+"rechargeList_detail.html";
    }
	/**
	 * 人工充值
	 */
	@RequestMapping(value = "/manual")
	public Object rechargeManual(String recharge_no, Model model){
		return PREFIX+MANUAL;
	}

	/**
	 * 人工充值手机号
	 */
	@RequestMapping(value = "/manual_ready")
	public Object manual_ready(String phone, Model model){
		RemoteUser user= remoteService.findUserById(phone);
		model.addAttribute("remoteUser",user);
		return PREFIX+MANUAL_READY;
	}
}
